package com.order.service.impl;

import java.util.List;
import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.order.domain.User;
import com.order.dao.IUserDao;
import com.order.dao.IContactDao;
import com.order.dao.ICustomerDao;
import com.order.domain.Customer;
import com.order.domain.PageBean;
import com.order.service.ICustomerService;

/**
 * 学生的业务层实现类
 * 
 * @author zhy
 *
 */
@Service("customerService")
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class CustomerServiceImpl implements ICustomerService {

	@Resource(name = "customerDao")
	private ICustomerDao customerDao;
	
	@Resource(name="userDao")
	private IUserDao userDao;
	
	@Resource(name = "contactDao")
	private IContactDao contactDao;

	@Override
	public List<Customer> findAllCustomer(DetachedCriteria dCriteria) {
		return customerDao.findAll(dCriteria);
	}
	
	/**
	 * 分页
	 */
	@Override
	public PageBean<Customer> findAllCustomer(DetachedCriteria dCriteria, int pageNumber, int pageSize) {
		//1.创建PageBean
		PageBean<Customer> pageBean = new PageBean<>(pageNumber, pageSize);
				
		//4.查询分页数据
		//4.1 查询总记录数（今日还未联系人数） 
		//实际语句 : select * from Linkman where 条件 limit ?,?  跟查询data的语句一致
		//目标语句 : select count(*) from Linkman where 条件
		//数据库一共50条数据   条件查询 男的数据一共25条   detachedCriteria里封装了条件
		//调换位置 成功 sql没有 limit 但是 查询不是count(*) 而是还是 *  需要设置投影查询
		//当设置了投影查询时 下面查询分页数据的代码 也变成count(*) 这不是我们想要的结果 我们想要的就是* 
		//以后将查询总记录数放在前面  将查询分页数据放在后面 理由 如果是查询分页数据  自动封装 开始索引 和每页显示个数 不能拆分出来
		//而投影查询是可以自己设置的  涉及到分页查询时 只需要将投影查询设置成null即可  会覆盖之前的投影
		dCriteria.setProjection(Projections.rowCount());
		int totalRecord = customerDao.findRecord(dCriteria);

		//4.2 查询分页的数据   
		//传入的参数 select * from Linkman where 条件 limit ?,?
		//传入离线对象 和 开始索引 每页显示个数时  自动将后俩个参数封装到离线对象中  在查询总记录数时 会出现异常
		dCriteria.setProjection(null);
		List<Customer> data = customerDao.findData(dCriteria , pageBean.getStartIndex() , pageSize);
        
		//3.赋值pageBean 
		pageBean.setData(data); //赋值分页数据
		pageBean.setTotalRecord(totalRecord);//赋值总记录数据
		
		
		//2.返回PageBean
		return pageBean;
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void saveCustomer(Customer customer) {
		customerDao.save(customer);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void removeCustomer(Integer studentId) {
		customerDao.delete(studentId);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public Customer findCustomerById(Integer studentId) {
		return customerDao.findById(studentId);
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void updateCustomer(Customer customer) {
		customerDao.update(customer);
	}

	@Override
	public List<User> findAllAffiliatedTeacher(DetachedCriteria uCriteria) {
		return userDao.findAll(uCriteria);
	}

	/**
	 * 拉出公海
	 */
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void pullCustomer(Integer studentId) {
		customerDao.updatePull(studentId);
		
	}

	/**
	 * 拉入公海
	 */
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void putCustomer(Integer studentId) {
		customerDao.updatePut(studentId);
		
	}

	/**
	 * 批量删除
	 */
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void deleteAll(int checkTnum) {
		customerDao.deleteAll(checkTnum);
		
	}

	/**
	 * 查询每个员工未联系学生人数
	 */
	@Override
	public int findContactNum(DetachedCriteria ddCriteria) {
		ddCriteria.setProjection(Projections.rowCount());
		return customerDao.findContactNum(ddCriteria);
	}

	/**
	 * 删除员工时，所属学生全部归为“公海”
	 */
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void updateAffiliatedTeache(Integer studentId) {
		customerDao.updateAffiliatedTeache(studentId);	
	}
	
}
